package com.zzw.testdemo01.handler;

import com.zzw.testdemo01.IdUtils;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.merchant.villager.VillagerEntity;
import net.minecraft.entity.passive.ChickenEntity;
import net.minecraft.entity.projectile.EggEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.MerchantOffer;
import net.minecraft.item.MerchantOffers;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = IdUtils.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class ProjectileEventHandler {

    // 注册事件监听器
    public ProjectileEventHandler() {
        MinecraftForge.EVENT_BUS.register(this);
    }


    //弹射物碰撞物品
    @SubscribeEvent
    public static void onProjectileImpactEvent(ProjectileImpactEvent event) {
        // 如果是蛋
        if (event.getEntity() instanceof EggEntity) {
            EggEntity egg =  (EggEntity) event.getEntity();
            // 获取碰撞位置
            Vector3d location = event.getRayTraceResult().getLocation();
            // 检查碰撞位置是否有火焰
            World world = egg.getCommandSenderWorld();
            BlockPos pos = new BlockPos(location);
            if (world.getBlockState(pos).is(BlockTags.FIRE)) {
                // 生成鸡
                ChickenEntity chicken = EntityType.CHICKEN.create(world);
                chicken.setPos(pos.getX(), pos.getY(), pos.getZ());
                // 设置为小鸡
                chicken.setBaby(true);
                world.addFreshEntity(chicken);
                // 删除鸡蛋
                egg.remove();
            }
        }

    }

}
